import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { GlobalStateType } from './global.typings'
import { removeFiles, getRegionSelectTreeList } from '~/api/global'

const global: Module<GlobalStateType, GlobalStateType> = {
  namespaced: true,
  state: {
    provinceSelectList: [],
    citySelectList: [],
    areaSelectList: []
  },
  getters: {
    provinceSelectList: (state) => state.provinceSelectList,
    citySelectList: (state) => state.citySelectList,
    areaSelectList: (state) => state.areaSelectList
  },
  mutations: {
    SET_PROVINCE_SELECT_LIST(state, payload) {
      state.provinceSelectList = payload?.length ? payload?.splice(1) : payload
      state.citySelectList = []
      state.areaSelectList = []
    },
    SET_CITY_SELECT_LIST(state, payload) {
      state.citySelectList = payload
      state.areaSelectList = []
    },
    SET_AREA_SELECT_LIST(state, payload) {
      state.areaSelectList = payload
    }
  },
  actions: {
    // 删除文件
    async RemoveFiles<S, T>(_: ActionContext<S, T>, params: T) {
      return await removeFiles<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取省市区下拉列表
    async GetRegionSelectTreeList<S, T>({ commit }: ActionContext<S, T>) {
      return await getRegionSelectTreeList().then((res: AxiosResponse) => {
        commit('SET_PROVINCE_SELECT_LIST', res?.data?.data || [])
        return res?.data
      })
    }
  }
}

export default global
